﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.HttpLoadBalancer.HttpProtocal;

namespace A2DFramework.HttpLoadBalancer.Parsers.ConcreteParsers
{
    class ResponseHeaderParser : IResponseHeaderParser
    {
        public HttpProtocal.ResponseStatusLine Parse(string rawHttp_FirstRow)
        {
            //HTTP/1.1 200 OK
            int spaceIndex1 = rawHttp_FirstRow.IndexOf(" ");
            int spaceIndex2 = rawHttp_FirstRow.IndexOf(" ", spaceIndex1 + 1);

            string versionCode = rawHttp_FirstRow.Substring(0, spaceIndex1).Trim();
            string statusCode = rawHttp_FirstRow.Substring(spaceIndex1, spaceIndex2 - spaceIndex1).Trim();
            string statusDescriptionCode = rawHttp_FirstRow.Substring(spaceIndex2).Trim();

            int status = int.Parse(statusCode);
            string description = statusDescriptionCode;
            HttpProtocalVersions version = Helper.ParseHttpVersion(versionCode);

            return new ResponseStatusLine()
            {
                StatusCode = status,
                StatusDescription = description,
                Version = version
            };
        }
    }
}
